package com.appspot.smartgan;

import com.google.android.gms.auth.GoogleAuthUtil;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
public class LoginActivity extends Activity {

	// Values for email and password at the time of the login attempt.
	private String mEmail;
	private String[] accountNames;
	
	// UI references.
	private Spinner mEmailView;
	private Button mLoginButton;
	
	// result intent
	private Intent resultIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		setContentView(R.layout.activity_login);

		// set action bar properties
		// get the action bar
		ActionBar actionBar = getActionBar();

		// create a transparent color for the action bar and set it
		ColorDrawable actionBarColorDrawable = new ColorDrawable();
		actionBarColorDrawable.setColor(getResources().
				getColor(android.R.color.transparent));
		actionBar.setBackgroundDrawable(actionBarColorDrawable);

		// delete action bar title and set new icon
		actionBar.setTitle("");
		actionBar.setIcon(R.drawable.ic_action_bar);
				
		// initialize the different view of the activity
		mEmailView = (Spinner) findViewById(R.id.accountsSpinner);
		mLoginButton = (Button) findViewById(R.id.login_button);
		
		// get all the Google account that are saves in the device
		accountNames = getAccountNames();
		
		// initialize spinner
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(LoginActivity.this,
				android.R.layout.simple_spinner_item, accountNames);
		mEmailView.setAdapter(adapter);
		
		mLoginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int index = mEmailView.getSelectedItemPosition();
				if (index < -1) {
					return;
				}
				mEmail = accountNames[index];
				
				resultIntent = new Intent();
				resultIntent.putExtra(MainActivity.PARENT_ID, mEmail);
				LoginActivity.this.setResult(Activity.RESULT_OK, resultIntent);
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		this.setResult(Activity.RESULT_CANCELED);
		finish();
	}
	
	private String[] getAccountNames() {
		AccountManager accountManager = AccountManager.get(this);
		Account[] accounts = 
				accountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
		 
		String[] names = new String[accounts.length];
		for (int i = 0; i < names.length; i++) {
			names[i] = accounts[i].name;
		}
		
		return names;
	}
	
}
